
#ifndef __FORWARD_SERVER_H
#define __FORWARD_SERVER_H

#include <apr.h>
#include <libssh2.h>
#include "../net/TCPServer.h"
#include "SSHChannel.h"

// One-thread should be used in the telescoping for security, only one
// session at a time - if that one session is not my session, the
// whole thing will stop

/** Implements a forwarding service to feed data to and from the SSH
 * channel. Essentially a front end for a data pipe that presents
 * itself as a server. */
class ForwardServer : public TCPServer
{
   public:
      /** Constructor for the forwarding server, set to a single
       * connection at once for security. */
      ForwardServer(int inothreads, int iportno, const char *ihostname, APRMutex *isshmutex, apr_pool_t *upperpool) :
         TCPServer(inothreads, iportno, ihostname, upperpool), sshmutex(isshmutex) {}
      /** Destructor for the forwarding server. */
      ~ForwardServer() {}
      void handleClientsOnThread(int threadid, apr_pool_t *localmp);
      static void *handleClientsOnThreadStatic(apr_thread_t *tthread, void *args);
      /** Get the client handler as a function pointer. */
      virtual functor getClientHandlerFunction() const { return (functor)(&handleClientsOnThreadStatic); }
      void setForwardingChannel(LIBSSH2_CHANNEL *ifchannel);
   private:
      /** Forwarding channel from libssh2. */
      LIBSSH2_CHANNEL *fchannel;
      /** Mutex for the SSH session. */
      APRMutex *sshmutex;
};

#endif
